package com.struts.action.message;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.spring.service.message.MessageService;
import com.struts.form.MessageForm;

public class MessageAction extends DispatchAction
{
	private MessageService messageService=null;

	public ActionForward findMessage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		int page=0;
		MessageForm messageform=(MessageForm)form;
		if(request.getParameter("page")!=null)
		{
			page=Integer.parseInt(request.getParameter("page"));
		}
		Map map=messageService.findMessage("from Message order by id desc",page);
		request.setAttribute("page_content", map.get("page_content"));
		request.setAttribute("message_list", map.get("message_list"));
		return mapping.findForward("MessageList");
	}
	
	public ActionForward delMessage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	{
		Long id=0L;
		int page=0;
		if(request.getParameter("id")!=null)
		{
			id=Long.parseLong(request.getParameter("id"));
		}
		if(request.getParameter("page")!=null)
		{
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(messageService.delMessage(id))
		{
			String path=mapping.findForward("success").getPath();
			ActionForward forward=new ActionForward(path+"&page="+page);
			forward.setRedirect(true);		
			return forward;
		}
		else
			return mapping.findForward("fail"); 
	}
	
	public ActionForward deleteBatchMessage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	{
		String ids="0";
		int page=0;
		if(request.getParameter("ids")!=null)
		{
			ids=request.getParameter("ids");
		}
		if(request.getParameter("page")!=null)
		{
			page=Integer.parseInt(request.getParameter("page"));
		}
		if (messageService.deleteBatchMessage(ids)) {
				String path = mapping.findForward("success").getPath();
				ActionForward forward= new ActionForward(path+"&page="+page);
				forward.setRedirect(true);
				return forward;
		} else {
			return mapping.findForward("fail");
		}
	}
	
	public ActionForward displayMessageInfo(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)
	{
		Long id=0L;
		if(request.getParameter("id")!=null)
		{
			id=Long.parseLong(request.getParameter("id"));
		}
		MessageForm messageform=messageService.displayMessageInfo(id);
		request.setAttribute("message",messageform );
		return mapping.findForward("display_message");
		
	}
	
	
	public MessageService getMessageService() {
		return messageService;
	}

	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
}
